package com.glory.dmegcpda.view

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.databinding.DataBindingUtil
import com.glory.dmegcpda.R
import com.glory.dmegcpda.databinding.ActivityBindBatteryBinding
import com.glory.dmegcpda.viewmodel.BindBatteryViewModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils
import kotlinx.android.synthetic.main.activity_bind_battery.*
import kotlinx.android.synthetic.main.activity_bind_battery.btnConfirm
import kotlinx.android.synthetic.main.activity_bind_battery.btnSubmit
import kotlinx.android.synthetic.main.activity_bind_battery.etLot
import kotlinx.android.synthetic.main.activity_bind_battery.etNum
import kotlinx.android.synthetic.main.activity_bind_battery.ivBack
import kotlinx.android.synthetic.main.activity_bind_battery.tvLotID
import kotlinx.android.synthetic.main.activity_bind_battery.tvMaterialID
import kotlinx.android.synthetic.main.activity_scribing.*

/**
 * FileName: BindBatteryActivity
 * Author: XJH
 * Date: 2021/9/27 14:46
 * Description:
 * History:
 */
class BindBatteryActivity : BaseActivity<BindBatteryViewModel>(), View.OnClickListener {
    var mBinding : ActivityBindBatteryBinding? = null
    override fun initData() {
        viewModel.defUI.callObserve.observe(this){
            tvMaterialID.text = ""
            tvLotID.text = ""
            etLot.setText("")
            etNum.setText("")
            etBox.setText("")
            etLot.requestFocus()
        }

        viewModel.defUI.startActivity.observe(this){
            finish()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_bind_battery)
        mBinding?.bindBatteryViewModel = viewModel
        ivBack.setOnClickListener(this)
        btnSubmit.setOnClickListener(this)
        btnConfirm.setOnClickListener(this)
//        FreeApi_EditTextUtils.setEditTextListener(etLot, this) {
//            val split = it.split("|")
//            if (split.isEmpty() || split.size < 3){
//                viewModel.defUI.toastEvent.value = ToastMessageBean("扫描编码格式有误，请重新扫描",false)
//                etLot.setText("")
//                etLot.requestFocus()
//                return@setEditTextListener
//            }
//
//            tvMaterialID.text = split[0]
//            tvLotID.text = split[1]
//        }

        etLot.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(text: Editable) {
                val toString = text.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    etLot.setText(toString.replace("\r", "").replace("\n", ""));
                    val split = text.split("|")
                    if (split.isEmpty() || split.size < 3){
                        viewModel.defUI.toastEvent.value = ToastMessageBean("扫描编码格式有误，请重新扫描",false)
                        etLot.setText("")
                        etLot.requestFocus()
                        return
                    }

                    tvMaterialID.text = split[0]
                    tvLotID.text = split[1]
                }
            }

        })

        etBox.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(text: Editable) {
                val toString = text.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    etBox.setText(toString.replace("\r", "").replace("\n", ""));
                }
            }

        })
    }

    override fun layoutId(): Int {
        return R.layout.activity_bind_battery
    }

    override fun onClick(p: View?) {
        when(p?.id){
            R.id.ivBack -> finish()
            R.id.btnSubmit ->{
                if (viewModel.mBatteryItems.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先在列表中添加数据",false)
                    return
                }

                viewModel.submit()
            }
            R.id.btnConfirm ->{
                if (etLot.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请扫描绿码",false)
                    etLot.setText("")
                    etLot.requestFocus()
                    return
                }

                if (etNum.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请输入数量",false)
                    etNum.setText("")
                    etNum.requestFocus()
                    return
                }

                if (etBox.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请扫描箱号或包号",false)
                    etBox.setText("")
                    etBox.requestFocus()
                    return
                }

                viewModel.addData(etBox.text.toString().trim(),etLot.text.toString().trim(),etNum.text.toString())
            }


        }
    }
}